<?php
		session_start();
define('TEMPLATES', '../templates/');
define('JS','../js/');
define('CSS','../css/');
define('OBJETOS','../objetos/');
define('NEGOCIOS','../negocio/');
define('EXTENSION','.phtml');
function __autoload($class_name){
  require_once $class_name . '.php';
}
 
Class TransaccionEstancia {
	
	var $AdminObj;
	var $generador_xhtml;
	var $vars;
	
	function TransaccionEstancia($array_vars){
		$this->AdminObj=new AdminEstancia();
		$this->vars=$array_vars;
		$this->generador_xhtml=new GeneradorXHTML();
	}

	function transNewEstancia(){
		$Estancia=$this->AdminObj->getNewObject("Estancia",array(Estancia));

		$check_caracteristicas=$this->generador_xhtml->generarChecks($this->AdminObj->getCaracteristicas());
		$this->generador_xhtml->mostrarTpl($Estancia,array("tpl"=>"Estancia"  ,"boton"=>"Continuar","accion"=>"transSetEstancia","css"=>"Estancia","js"=>"Estancia.js,Validator.js", "checks"=>$check_caracteristicas));
	}
	
	function transSetEstancia(){
		$this->vars['id_estancia']=$this->AdminObj->getNewID("estancia","id_Estancia");
		$this->vars['id_hotel_estancia']=$_SESSION['S_HOTEL'];
		$Estancia=$this->AdminObj->getNewObject("Estancia",$this->vars);
		$this->AdminObj->setEstancia($Estancia);
		
		$codigos=split(';',$this->vars['ids_caracteristicas']);
		foreach($codigos as $id_c){
			$id_estancia_caracteristica=$this->AdminObj->getNewID("Estanciacaracteristica", "id_estanciacaracteristica");
			$this->AdminObj->setCaracteristicas($id_estancia_caracteristica, $id_c, $this->vars['id_estancia']);
		}
		$this->AdminObj->redireccionar("TransaccionEstancia.php?a=transGetEstancia&id_estancia=".$this->vars['id_estancia']);
		
	}
    
    function transSetTarifa(){
    	$this->vars['id_tarifa']=$this->AdminObj->getNewID("tarifa","id_tarifa");
		$this->vars['id_temporada']=$this->AdminObj->getNewID("temporada","id_temporada");
		$this->vars['id_temporada_tarifa']=$this->vars['id_temporada'];
    	switch($this->vars['tipo_temporada']){
    		case 'general': {
    			$mes=array("January"=>'01', "February"=>'02', "March"=>'03', "April"=>'04', "May"=>'05', "June"=>'06', "July"=>'07', "August"=>'08', "September"=>'09', "October"=>'10', "November"=>'11', "December"=>'12');
				$this->vars['fini_temporada']=$mes[current(split('-',$this->vars['fini_temporada']))]."-".next(split('-',$this->vars['fini_temporada']));	
				$this->vars['ffin_temporada']=$mes[current(split('-',$this->vars['ffin_temporada']))]."-".next(split('-',$this->vars['ffin_temporada']));
    			$this->vars['fini_temporada']='0001-'.$this->vars['fini_temporada'];
    			$this->vars['ffin_temporada']='0001-'.$this->vars['ffin_temporada'];} break;
    		case 'defecto': {$this->vars['fini_temporada']='NULL'; $this->vars['ffin_temporada']='NULL';} break;
    	}
    	$this->AdminObj->setObjectDB($this->AdminObj->getNewObject("Temporada", $this->vars));
    	$this->AdminObj->setObjectDB($this->AdminObj->getNewObject("Tarifa", $this->vars));
        
		echo $this->vars['id_temporada'];
    }
    function transUpdateTarifa(){
    	$tarifa=$this->AdminObj->getTarifaFromTemporada($this->vars['id_temporada']);
    	$this->vars['id_tarifa']=$tarifa->id_tarifa;
		$this->vars['id_temporada_tarifa']=$this->vars['id_temporada'];
		
    	switch($this->vars['tipo_temporada']){
    		case 'general': {
    			$mes=array("January"=>'01', "February"=>'02', "March"=>'03', "April"=>'04', "May"=>'05', "June"=>'06', "July"=>'07', "August"=>'08', "September"=>'09', "October"=>'10', "November"=>'11', "December"=>'12');
				$this->vars['fini_temporada']=$mes[current(split('-',$this->vars['fini_temporada']))]."-".next(split('-',$this->vars['fini_temporada']));	
				$this->vars['ffin_temporada']=$mes[current(split('-',$this->vars['ffin_temporada']))]."-".next(split('-',$this->vars['ffin_temporada']));
    			$this->vars['fini_temporada']='0001-'.$this->vars['fini_temporada'];
   				 $this->vars['ffin_temporada']='0001-'.$this->vars['ffin_temporada'];} break;
    		case 'defecto': {$this->vars['fini_temporada']='NULL'; $this->vars['ffin_temporada']='NULL';} break;
    	}
    	$this->AdminObj->updateObject($this->AdminObj->getNewObject("Temporada", $this->vars));
    	$this->AdminObj->updateObject($this->AdminObj->getNewObject("Tarifa", $this->vars));
		echo $this->vars['id_temporada'];
    }
    
        
    function transSetHabitacion(){
        $this->vars['id_habitacion']=$this->AdminObj->getNewID("habitacion", "id_habitacion");
        $this->vars['id_estancia_habitacion']=$this->vars['id_estancia'];
        $this->vars['id_hotel_habitacion']=$_SESSION['S_HOTEL'];
        $Hab=$this->AdminObj->getNewObject("Habitacion", $this->vars);
        $this->AdminObj->setObjectDB($Hab, "Habitacion");
        echo $this->vars['id_habitacion'];
    }
    function transUpdateHabitacion(){
        $this->vars['id_estancia_habitacion']=$this->vars['id_estancia'];
        $this->vars['id_hotel_habitacion']=$_SESSION['S_HOTEL'];
        $this->AdminObj->updateObject($this->AdminObj->getNewObject("Habitacion", $this->vars), "Habitacion");
        echo $this->vars['id_habitacion'];
    }

	
	function transGetEstancia(){
		$Estancia=$this->AdminObj->getEstancia($this->vars['id_estancia']);
		$check_caracteristicas=$this->generador_xhtml->generarChecks($this->AdminObj->getCaracteristicas());
		$caracteristicas=$this->generador_xhtml->generarImgs($this->AdminObj->getCaracteristicasEstancia($Estancia->id_estancia));
		$rows_especificas=$this->generador_xhtml->generarRowsTableX($this->AdminObj->getTemporadasTarifa($this->vars['id_estancia'], "especifica"),false,0, 'button', "Editar", 'jqm_esp', 'button', "Eliminar", 'del_esp', "checkbox", "check_especifica", 'check_esp');
		$rows_generales=$this->generador_xhtml->generarRowsTableX($this->AdminObj->getTemporadasTarifa($this->vars['id_estancia'], "general"),false,0, 'button', "Editar", 'jqm_gen', 'button', "Eliminar", 'del_gen', "checkbox", "check_general", 'check_gen');
		$rows_defecto=$this->generador_xhtml->generarRowsTableX($this->AdminObj->getTemporadasTarifa($this->vars['id_estancia'], "defecto"),false,0, 'button', "Editar", 'jqm_def', 'button', "Eliminar", 'del_def', "checkbox", "check_defecto", 'check_def');	
		$rows_altas=$this->generador_xhtml->generarRowsTableX($this->AdminObj->getAltas($this->vars['id_estancia']),false,0, 'button', "Editar", 'jqm_hab', 'button', "Eliminar", 'del_hab', "checkbox", "check_habitacion", 'check_hab');
		$array_elementos=array("tpl"=>"EstanciatarifaAlta", "titulo"=>"".$Estancia->nombre_estancia ,"css"=>"Estancia","js"=>"Estancia2.js,Validator.js", "id_estancia"=>$this->vars['id_estancia'], "rows_especificas"=>$rows_especificas, "rows_generales"=>$rows_generales, "rows_defecto"=>$rows_defecto, "rows_altas"=>$rows_altas, "caracteristicas"=>$caracteristicas, "checks"=>$check_caracteristicas);
		$this->generador_xhtml->mostrarTpl($Estancia,$array_elementos);
	}
	
	function transUpdateEstancia(){
		$this->vars['id_hotel_estancia']=$_SESSION['S_HOTEL'];
		$Estancia=$this->AdminObj->getNewObject("Estancia",$this->vars);
		$this->AdminObj->updateEstancia($Estancia);
		$this->AdminObj->delCaracteristicas($this->vars['id_estancia']);
		$codigos=split(';',$this->vars['ids_caracteristicas']);
		foreach($codigos as $id_c){
			$id_estancia_caracteristica=$this->AdminObj->getNewID("Estanciacaracteristica", "id_estanciacaracteristica");
			$this->AdminObj->setCaracteristicas($id_estancia_caracteristica, $id_c, $this->vars['id_estancia']);
		}
		echo($this->generador_xhtml->generarImgs($this->AdminObj->getCaracteristicasEstancia($Estancia->id_estancia)));
	}
	
	function transDeleteEstancia(){
		$this->AdminObj->deleteEstancia($this->AdminObj->getEstancia($this->vars['id_estancia']));
		echo($this->vars['id_estancia']);
	}
	function transGetEstancias()
	{
		$rows=$this->generador_xhtml->generarRowsTableX($this->AdminObj->getEstancias(),false,0, 'button', "Ver", 'ver_estancias', 'button', "Eliminar", 'del_estancias', "checkbox", "check", 'check_estancias');
		$this->generador_xhtml->mostrarTpl("",array("tpl"=>"Estanciastable", "titulo"=>"ESTANCIAS", "rows"=>$rows,"js"=>"Estanciatable.js,browser.js"));
	}		
	function transDeleteObjeto(){
		$tabla=$this->vars['tabla'];
		if($tabla=='lista_tarifas_especifica' || $tabla=='lista_tarifas_general' || $tabla=='lista_tarifas_defecto') $tabla="temporada";
		if($tabla=='lista_habitacion') $tabla="habitacion";
		$this->AdminObj->deleteObject($this->AdminObj->getObjeto($tabla, $this->vars['id_tabla']), $tabla);
		echo($this->vars['id_tabla']);
	}
	function transDeleteObjetos(){
		$codigos=split(";", $this->vars['ids']);
		$tabla=$this->vars['tabla'];
		if($tabla=='lista_tarifas_especifica' || $tabla=='lista_tarifas_general' || $tabla=='lista_tarifas_defecto') $tabla="temporada";
		if($tabla=='lista_habitacion') $tabla="habitacion";
		foreach($codigos as $id_o){
			$this->AdminObj->deleteObject($this->AdminObj->getObjeto($tabla, $id_o), $tabla);
		}
	}
	
}

if($_GET){
	$vars_trans=array_slice($_GET,1);
//antes no olvidar poner una validador para que nadie pueda ejecutar las funciones
	call_user_func(array(new TransaccionEstancia($vars_trans),current($_GET)?current($_GET):"transGetEstancias"));
}
else if($_POST){
		$vars_trans=array_slice($_POST,1);
		call_user_func(array(new TransaccionEstancia($vars_trans),current($_POST)?current($_POST):"transGetEstancias"));
		}
	else	call_user_func(array(new TransaccionEstancia(array()),"transGetEstancias"));

?>